﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace nMongo
{
    public partial class MongoRepository<T>
    {
        /// <summary>
        /// Retrieves all the objects inside the current collection.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> Select()
        {
            IEnumerable<T> returned = CollectionQuery.ToList();

            if (returned.Count() > 0)
            {
                foreach (T item in returned)
                {
                    this.AutoDetectPersistencyByObject(MongoPersistencyState.Retrieved, item);
                }
            }
            return returned;
        }

        /// <summary>
        /// Retrieves all the objects inside the current collection, giving a specified condition.
        /// </summary>
        /// <param name="predicate">Expression to evaluate and determines the condition satisfaction.</param>
        public IEnumerable<T> Select(Expression<Func<T, bool>> predicate)
        {
            IEnumerable<T> returned = CollectionQuery.Where(predicate).ToList();

            if (returned.Count() > 0)
            {
                foreach (T item in returned)
                {
                    this.AutoDetectPersistencyByObject(MongoPersistencyState.Retrieved, item);
                }
            }
            return returned;
        }
    }
}
